package com.uniandes.edu.co.interfaz.ciclo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JLabel;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.uniandes.edu.co.controlador.ControladorCiclo;
import com.uniandes.edu.co.dto.Requerimiento;
import com.uniandes.edu.co.dto.Tsp;
import com.uniandes.edu.co.excepciones.ExcepcionExiste;
import com.uniandes.edu.co.excepciones.ExcepcionParseInt;
import com.uniandes.edu.co.excepciones.ExcepcionVacio;



/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class InterfazAgregarCiclo extends javax.swing.JFrame {
	private JScrollPane jScrollPane1;
	private JLabel jLabel5;
	private JScrollPane jScrollPane2;
	private JTextArea jTextArea2;
	private JScrollPane jScrollPane3;
	private JTextArea jTextArea1;
	private JButton jButton2;
	private JLabel jLabel6;
	private JButton jButton4;
	private JButton jButton3;
	private JButton jButton1;
	private JButton jButton5;
	private JLabel jLabel4;
	private JLabel jLabel3;
	private JTextField jTextField1;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private LinkedList<Requerimiento> listadoR;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(final String[] args, final LinkedList<Requerimiento> listadoRequerimientos) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				InterfazAgregarCiclo inst = new InterfazAgregarCiclo(listadoRequerimientos,args);
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	public InterfazAgregarCiclo(LinkedList<Requerimiento> listadoRequerimientos,String[] args) {
		super();
		initGUI(listadoRequerimientos,args);

	}


	private void initGUI(LinkedList<Requerimiento> listadoRequerimientos,String[] args ) {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			if (listadoRequerimientos==null)
				listadoR= new LinkedList<Requerimiento>();
			else
				listadoR=listadoRequerimientos;
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				final DefaultTableModel dm = new DefaultTableModel();

				dm.setDataVector(ControladorCiclo.getSingleton().tablaRequerimientos(listadoR), ControladorCiclo.getSingleton().encabezadosRequerimientos());
				
				TableModel jTable1Model =dm;
				JTable jTable1 = new JTable(dm) {
					public void tableChanged(TableModelEvent e) {
						super.tableChanged(e);
						repaint();
					}
				};

				ButtonGroup group1 = new ButtonGroup();
				for (int i = 0; i < dm.getRowCount(); i++)
				{
					group1.add( (AbstractButton) dm.getValueAt(i, 0));
				}
				jTable1.setModel(jTable1Model);

				jTable1.getColumn("Seleccionar").setCellRenderer(
						new RadioButtonRenderer());
				jTable1.getColumn("Seleccionar").setCellEditor(
						new RadioButtonEditor(new JCheckBox()));
				jScrollPane1.setBounds(40, 345, 386, 228);
				jScrollPane1.setViewportView(jTable1);
				jTable1.setBounds(50, 149, 383, 225);

				{
					jButton3 = new JButton();
					getContentPane().add(jButton3);
					jButton3.setText("Modificar");
					jButton3.setBounds(336, 310, 90, 21);
					jButton3.addActionListener(new ActionListener() {
						private boolean bandera=true;

						public void actionPerformed(ActionEvent evt) {
							for (int i = 0; i < dm.getRowCount(); i++)
                            {
                                if (((AbstractButton) dm.getValueAt(i, 0)).isSelected())
                                {
                                    dispose();                               
                                    InterfazModificarRequerimiento.main(null,listadoR,String.valueOf(dm.getValueAt(i, 1)) );
                                    bandera=false;
                                }
                            }
                            if (bandera){
                                JOptionPane.showMessageDialog(null, "Seleccione un elemento");
                            }
						}
					});
				}
				
				{
					jButton4 = new JButton();
					getContentPane().add(jButton4);
					jButton4.setText("Eliminar");
					jButton4.setBounds(235, 309, 88, 23);
					jButton4.addActionListener(new ActionListener() {
						private boolean bandera=true;

						public void actionPerformed(ActionEvent evt) {
							for (int i = 0; i < dm.getRowCount(); i++)
	                        {
	                            if (((AbstractButton) dm.getValueAt(i, 0)).isSelected())
	                            {
	                                dispose();                               
	                                try {
										Tsp.getSingleton().eliminarRequerimiento(String.valueOf(dm.getValueAt(i, 1)), listadoR);
										dispose();
										JOptionPane.showMessageDialog(null, "Se ha eliminado correctamente un elemento");
										InterfazAgregarCiclo.main(null, listadoR);
	                                //} catch (ExcepcionExiste e) {
	                                } catch (Exception e) {
										JOptionPane.showMessageDialog(null, e.getMessage());
									} 
	                                bandera=false;
	                            }
	                        }
	                        if (bandera){
	                            JOptionPane.showMessageDialog(null, "Seleccione un elemento");
	                        }
						}
					});
				}

			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("CICLO");
				jLabel1.setBounds(46, 12, 386, 26);
				jLabel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jLabel1.setFont(new java.awt.Font("Tahoma",1,14));
				jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Id Ciclo");
				jLabel2.setBounds(45, 51, 142, 20);
				jLabel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Estrategia");
				jLabel3.setBounds(45, 80, 142, 20);
				jLabel3.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}


			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setBounds(40, 296, 386, 8);
				jLabel4.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Agregar");
				jButton1.setBounds(336, 270, 90, 21);
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						InterfazRequerimiento.main(new String[]{jTextField1.getText(),jTextArea1.getText(),jTextArea2.getText()},listadoR);
						dispose();
					}
				});
			}
			{
				jButton5 = new JButton();
				getContentPane().add(jButton5);
				jButton5.setText("Regresar");
				jButton5.setBounds(313, 586, 113, 21);
				jButton5.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
						InterfazCiclo.main(null);
					}
				});
			}
			{
				jLabel5 = new JLabel();
				getContentPane().add(jLabel5);
				jLabel5.setText("Plan de desarrollo");
				jLabel5.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jLabel5.setBounds(44, 167, 142, 20);
			}
			{
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1);
				jTextField1.setBounds(205, 50, 226, 23);
			}
			
			{
				jLabel6 = new JLabel();
				getContentPane().add(jLabel6);
				jLabel6.setText("Requerimientos");
				jLabel6.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jLabel6.setBounds(40, 270, 142, 20);
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("Agregar Ciclo");
				jButton2.setBounds(182, 585, 120, 23);
				jButton2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try {
							Tsp.getSingleton().agregarNuevoCiclo(jTextField1.getText(), jTextArea1.getText(), jTextArea2.getText(), listadoR);
							JOptionPane.showMessageDialog(null, "Se ha adicionado correctamente un Ciclo");
							InterfazCiclo.main(null);
							dispose();
						} catch (ExcepcionExiste e) {
							JOptionPane.showMessageDialog(null, e.getMessage());
						} catch (ExcepcionVacio e) {
							JOptionPane.showMessageDialog(null, e.getMessage());
						}catch (ExcepcionParseInt e) {
							JOptionPane.showMessageDialog(null, e.getMessage());
						}
					}
				});
			}
			{
				jScrollPane2 = new JScrollPane();
				getContentPane().add(jScrollPane2);
				jScrollPane2.setBounds(205, 79, 226, 71);
				{
					jTextArea1 = new JTextArea();
					jScrollPane2.setViewportView(jTextArea1);
				}
			}
			{
				jScrollPane3 = new JScrollPane();
				getContentPane().add(jScrollPane3);
				jScrollPane3.setBounds(204, 167, 227, 83);
				{
					jTextArea2 = new JTextArea();
					jScrollPane3.setViewportView(jTextArea2);
				}
			}
			if (args!=null)
			{
				jTextField1.setText(args[0]);
				jTextArea1.setText(args[1]);
				jTextArea2.setText(args[2]);
			}
			pack();
			this.setSize(477, 673);

		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}



}
